﻿///***************************************************************
/// 시  스  템 : Updms.Etc
/// 단위시스템 : ETC
/// 계      층 : Presentation Layer
/// 프로그램명 : Etc2010r
/// 파  일  명 : Etc2010r.aspx.cs
/// 설      명 : 레고현황
///***************************************************************
/// 수정ID 일자       이름     내용
///---------------------------------------------------------------
/// CH001  2014-09-15 송시명   최초 작성
///***************************************************************
using System;
using System.Data;
using System.Collections;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using SMFramework.Data;
using SMFramework.Utility;
using SMFramework.Web.UI.WebControls.Page;
using SMFramework.Web.UI.WebControls.SMGridView;

using Base.AppFramework.Web.UI.CommandBar;
using Base.Dac;

using Updms.Etc.Exe;


namespace Updms.Etc
{
	public partial class Etc2010r : Updms.Common.BaseClass.PageBase
	{
		#region 전역영역

		public static readonly string PID = "Etc2010r";
		public static readonly string gs_path_info = "/updms/Ref/" + PID + ".aspx";

		Global global = new Global();

		//메뉴 선택값
		protected string gs_sm = "";

		//시리즈별 보유수 합계
		decimal ld_cnt = 0;
		decimal ld_price = 0;
		decimal ld_brick_cnt = 0;
		decimal ld_brick_weight = 0;
		decimal ld_figure_cnt = 0;

		#endregion

		#region Page_Load, OnPageLoad

		protected void Page_Load(object sender, EventArgs e)
		{
			SetInit();
			if (!Page.IsPostBack)
			{
				// 최초 화면 로딩시 처리해야 할 부분
				OnPageLoad();
				SearchProcess();
			}
		}

		// OnPageLoad
		private void OnPageLoad()
		{
			SetLiterals(global.GetLabelInfoByPgmId(PID, LANG_SET));
			SetSearchParameter();
		}

		#endregion

		#region InitializeComponent

		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);
			this.PreRender += new System.EventHandler(this.Page_PreRender);
		}

		#endregion

		#region SetInit, SetSearchParameter

		//초기값설정
		private void SetInit()
		{
			lbl_search_flag.Text = "INIT";

			gs_sm = (Request.QueryString["sm"] == null || Request.QueryString["sm"].Trim().Length != 7)
					? global.GetDirDepth(PID) : Request.QueryString["sm"].ToString();
		}

		//검색파라미터
		private void SetSearchParameter()
		{
		}

		#endregion

		#region 마스터 Method ()

		#endregion

		#region 디테일 Method ()

		#endregion

		#region 챠트

		#region 시리즈별 보유수
		//시리즈별 보유수
		private void SetSeries(DataSet lo_ds, string dt_name)
		{
			GridView1.DataSource = lo_ds.Tables[dt_name];
			GridView1.DataBind();
		}
		#endregion

		#region 요일별 구입수
		//요일별 구입수
		private void DrawChartBuyCntByWeekday(DataSet lo_ds, string dt_name)
		{
			//초기화
			chart_buy_cnt_by_weekday.Data.Clear();

			int li_count = DataSetUtil.GetRowCount(lo_ds, dt_name);

			for (int i = 0; i < li_count; i++)
			{
				string ls_label = DataSetUtil.GetFldStr(lo_ds, dt_name, i, "Week_Day")
				+ "[" + ConversionUtil.AddComma(DataSetUtil.GetFldStr(lo_ds, dt_name, i, "Total_Cnt"))
				+ "][" + ConversionUtil.AddComma(DataSetUtil.GetFldStr(lo_ds, dt_name, i, "Total_Price")) + "]";

				chart_buy_cnt_by_weekday.Data[0, i] = DataSetUtil.GetFldInt(lo_ds, dt_name, i, "Total_Cnt");
				chart_buy_cnt_by_weekday.AxisX.Labels[i] = ls_label;
			}
		}
		#endregion

		#region 최근구입정보

		private void SetRecentBuyList(DataSet lo_ds, string dt_name)
		{
			GridView2.DataSource = lo_ds.Tables[dt_name];
			GridView2.DataBind();
		}

		#endregion

		#region 년도별 구입수
		//년도별 구입수
		private void DrawChartBuyByYearly(DataSet lo_ds, string dt_name)
		{
			int li_count = 0;
			li_count = DataSetUtil.GetRowCount(lo_ds, dt_name);

			//초기화
			chart_buy_by_yearly.Data.Clear();

			for (int i = 0; i < li_count; i++)
			{
				//수동으로 데이터 바인딩
				chart_buy_by_yearly.Data[0, i] = DataSetUtil.GetFldInt(lo_ds, dt_name, i, "Total_Cnt");
				chart_buy_by_yearly.Data[1, i] = DataSetUtil.GetFldDbl(lo_ds, dt_name, i, "Total_Price");

				//X축 라벨링
				chart_buy_by_yearly.AxisX.Labels[i] = DataSetUtil.GetFldStr(lo_ds, dt_name, i, "Buy_Year");

				//0은 안보여준다
				chart_buy_by_yearly.Points[0, i].PointLabels.Visible = (chart_buy_by_yearly.Data[0, i] > 0);
				chart_buy_by_yearly.Points[1, i].PointLabels.Visible = (chart_buy_by_yearly.Data[1, i] > 0);
			}
		}

		#endregion

		#region 검색
		//검색
		private void SearchProcess()
		{
			DataPack lo_dp = new DataPack();
			DataSet lo_ds = new DataSet();

			lbl_search_flag.Text = "SEARCH";
			SetSearchParameter();

			//챠트
			lo_dp.AddProperty("ls_lang_set", typeof(string), LANG_SET);

			//DataTable명 지정
			string[] dt_name = new string[] {	"ETC2010R_SERIES",
												"ETC2010R_WEEKDAY",
												"ETC2010R_RECENT",
												"ETC2010R_YEARLY"
											};
			using (Etc2010r_NT _Etc2010r_NT = new Etc2010r_NT())
			{
				lo_ds.Merge(_Etc2010r_NT.GetTotalLegoReport(lo_dp, dt_name));
			}

			SetSeries(lo_ds, dt_name[0]);
			DrawChartBuyCntByWeekday(lo_ds, dt_name[1]);
			SetRecentBuyList(lo_ds, dt_name[2]);
			DrawChartBuyByYearly(lo_ds, dt_name[3]);
		}
		#endregion

		#endregion

		#region Page_PreRender, ChangePageMode

		// Page_PreRender
		private void Page_PreRender(object sender, System.EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				using (AccessCheck access = new AccessCheck())
				{
					//접근권한 체크
					if (!access.CheckPgmAccessGrant(PID, USER_ID, ACCESS_LOG_INFO))
					{
						Response.Redirect(URL_NO_AUTHORITY_REDIRECT_BASE);	//사용자의 권한은 요청하신 페이지를 볼 수 없습니다
					}
					//사용권한별 버튼처리
					PagePreRender.SetVisibleCtrl(this, access.CheckPgmUsingGrant(USER_DIV, PID), access.GetVisibleCtrl(PID));
				}
			}
			ChangePageMode();
		}

		//ChangePageMode
		private void ChangePageMode()
		{
			SetGridViewHeader(global.GetColumnListByPgmId(PID, LANG_SET));
		}

		#endregion

		#region GridView이벤트

		protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			if (e.Row.RowType == DataControlRowType.DataRow)
			{
				//마우스올렸을 때
				e.Row.Attributes.Add("onMouseOver", GRID_ONMOUSEOVER_COLOR);
				e.Row.Attributes.Add("onMouseOut", GRID_ONMOUSEOUT_COLOR);

				//페이지별 합계
				ld_cnt += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "Total_Cnt"));
				ld_price += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "Total_Price"));
				ld_brick_cnt += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "Total_Brick_Cnt"));
				ld_brick_weight += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "Total_Brick_Weight"));
				ld_figure_cnt += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "Total_Figure_Cnt"));
			}

			//Footer에 합계 바인딩
			if (e.Row.RowType == DataControlRowType.Footer)
			{
				e.Row.Cells[0].Text = "Total";
				e.Row.Cells[0].Attributes.Add("class", "total");
				e.Row.Cells[1].Text = ld_cnt.ToString("#,###,##0");
				e.Row.Cells[1].Attributes.Add("class", "money");
				e.Row.Cells[2].Text = ld_price.ToString("#,###,##0");
				e.Row.Cells[2].Attributes.Add("class", "money");
				e.Row.Cells[3].Text = ld_brick_cnt.ToString("#,###,##0");
				e.Row.Cells[3].Attributes.Add("class", "money");
				e.Row.Cells[4].Text = ld_brick_weight.ToString("#,###,##0.##0");
				e.Row.Cells[4].Attributes.Add("class", "money");
				e.Row.Cells[5].Text = ld_figure_cnt.ToString("#,###,##0");
				e.Row.Cells[5].Attributes.Add("class", "money");
			}
		}

		protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			if (e.Row.RowType == DataControlRowType.DataRow)
			{
				//마우스올렸을 때
				e.Row.Attributes.Add("onMouseOver", GRID_ONMOUSEOVER_COLOR);
				e.Row.Attributes.Add("onMouseOut", GRID_ONMOUSEOUT_COLOR);

				//번호
				e.Row.Cells[0].Text = (e.Row.RowIndex + 1).ToString();
			}

			//머지
			SMGridView sGv = new SMGridView(this.Page, ref GridView2);
			sGv.RowSpanGridView(GridView2, 1); //시리즈
		}

		#endregion

		#region 버튼이벤트

		//검색
		protected void ibtn_chart_Click(object sender, EventArgs e)
		{
			SearchProcess();
		}

		#endregion
	}
}
